package com.jf.ai.service;

import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2025-07-04
 * Time:20:08
 */
@Service
public class ElasticsearchService {
    @Resource
    private VectorStore vectorStore;

    public void addDocument() {
        List<Document> documents = List.of(
                new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta2")),
                new Document("The World is Big and Salvation Lurks Around the Corner"),
                new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));

        // Add the documents to Elasticsearch
        vectorStore.add(documents);
    }

    public List<Document> search(String query) {
        // Search for documents
        return vectorStore.similaritySearch(SearchRequest.builder()
                .query(query)
                //.filterExpression(new FilterExpressionBuilder().eq("meta1", "meta2").build())
                .filterExpression("'meta1' == 'meta2'")
                .similarityThreshold(0.5)
                .topK(5)
                .build());
    }


}
